﻿#region Using Directives

using System;
using System.Collections.Generic;

#endregion

namespace GeekRule.ORM.DataMapping
{
    /// <summary>
    /// Singleton used to cache entity mappings to prevent redundant reflection calls
    /// </summary>
    public class DataMappingCache
    {
        #region Private Members

        private static volatile DataMappingCache _instance = null;
        private static object _syncRoot = new object();
        private Dictionary<Type, EntityMapping> _entityMappings;

        #endregion

        #region Public Accessors

        /// <summary>
        /// Cache of EntityMappings
        /// </summary>
        public Dictionary<Type, EntityMapping> EntityMappings
        {
            get { return _entityMappings; }
            set { _entityMappings = value; }
        }

        #endregion

        #region Constructor

        private DataMappingCache()
        {
            _entityMappings = new Dictionary<Type, EntityMapping>();
        }

        #endregion

        #region Singleton Accessor

        public static DataMappingCache Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncRoot)
                    {
                        if (_instance == null)
                        {
                            _instance = new DataMappingCache();
                        }
                    }
                }

                return _instance;
            }
        }

        #endregion
    }
}
